package com.atguigu.day08;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink07_SQL_KafkaToKafka {
    public static void main(String[] args) {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //TODO 创建一张表连接kafka的topic，进而消费这个topic中的数据
        tableEnv.executeSql("create table kafkaSource(\n" +
                "    id STRING,\n" +
                "    ts BIGINT,\n" +
                "    vc INT\n" +
                ") with (\n" +
                "    'connector' = 'kafka',\n" +
                "    'topic' = 'topic_source_sensor',\n" +
                "    'properties.bootstrap.servers' = 'hadoop102:9092',\n" +
                "    'properties.group.id' = '220509',\n" +
                "    'scan.startup.mode'= 'latest-offset',\n" +
                "    'format' = 'csv'\n" +
                ")");

        //TODO 创建一张表连接kafka的topic，进而往这个topic中写数据
        tableEnv.executeSql("create table kafkaSink(\n" +
                "    id STRING,\n" +
                "    ts BIGINT,\n" +
                "    vc INT\n" +
                ") with (\n" +
                "    'connector' = 'kafka',\n" +
                "    'topic' = 'topic_sink_sensor',\n" +
                "    'properties.bootstrap.servers' = 'hadoop102:9092',\n" +
                "    'format' = 'csv'\n" +
                ")");

        tableEnv.executeSql("insert into kafkaSink  select * from kafkaSource");
    }
}
